%{
    if(_value){
    	currentObject = _value
    	
    	if(_type){
    		if(_type instanceof String)
    			currentType = play.Play.classloader.loadClass(_type)
    		else if(_type instanceof Class)
    			currentType = _type	
	    }
	    else{
	    	currentType = _value?.getClass()
	    }
    }
    else if(_type){
    	if(_type instanceof String)
   			currentType = play.Play.classloader.loadClass(_type)
   		else if(_type instanceof Class)
   			currentType = _type	   
	}

    // Eval fields tags
    def fieldsHandler = [:]
    if(_body) {
        _body.setProperty('fieldsHandler', fieldsHandler);
        _body.toString(); // we skeep the real result ...
    }
}%
<div class="crudSubForm">

#{list items: controllers.CRUD.ObjectType.ObjectField.getFields(currentType), as:'field'}

    %{
       am = ''
       //def field = currentType.getField(fieldName)
       def fieldName = field?.name;
       def showField = true
     }%
    %{ if(fieldsHandler[fieldName]) { }%
        <div class="crudField crud_${field.type}">
        %{
            def handler = fieldsHandler[fieldName]
            handler.setProperty('fieldName', 'root.' + fieldName + (field?.type == 'relation' ? '@id' : ''))
            def oldObject = handler.getProperty('object')
            handler.setProperty('root', currentObject)
            out.println(handler.toString())
            handler.setProperty('root', oldObject)
            handler.setProperty('fieldName', null)
        }%
        </div>
    %{ } else { }%
        #{if field.type != 'unknown'}
            %{ showField = false }%

            <div class="crudField crud_${field.type}">

                #{ifnot field}
                    %{ throw new play.exceptions.TagInternalException('Field not found -> ' + fieldName) }%
                #{/ifnot}

                #{if field.type == 'text'}
                    #{crud.textField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                #{/if}

                #{if field.type == 'password'}
                    #{crud.passwordField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                #{/if}

                #{if field.type == 'file'}
                    #{crud.fileField name:field.name, value:(currentObject ? currentObject[field.name] : null), id:currentObject?.id, prefix:_prefix /}
                #{/if}

                #{if field.type == 'longtext'}
                    #{crud.longtextField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                #{/if}

                #{if field.type == 'number'}
                    #{crud.numberField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                    %{ am = 'crud.help.numeric' }%
                #{/if}

                #{if field.type == 'date'}
                    #{crud.dateField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                    %{ am = 'crud.help.dateformat' }%
                #{/if}

                #{if field.type == 'relation'}
                    #{crud.relationField name:field.name, value:(currentObject ? currentObject[field.name] : null), field:field, prefix:_prefix /}
                #{/if}

                #{if field.type == 'boolean'}
                    #{crud.checkField name:field.name, value:(currentObject ? currentObject[field.name] : null), prefix:_prefix /}
                #{/if}

                #{if field.type == 'enum'}
                    #{crud.enumField name:field.name, value:(currentObject ? currentObject[field.name] : null), property:field, prefix:_prefix /}
                #{/if}

                #{if field.type == 'serializedText'}
                    #{crud.textField name:field.name, value:(currentObject ? controllers.CRUD.collectionSerializer(currentObject[field.name]) : null), property:field, prefix:_prefix /}
                #{/if}

                #{if field.type == 'embedded'}
                    #{crud.embeddedField name:field.name, value:(currentObject ? currentObject[field.name] : null), field:field, prefix:_prefix /}
                #{/if}
                
                <span class="crudHelp">
                    &{am}
                        %{ play.data.validation.Validation.getValidators(currentType, fieldName, 'object').each() { }%
                            %{
                                switch (it.annotation.annotationType().name.substring(21)) {
                                    case "Required":
                                        out.println(messages.get('crud.help.required'))
                                        break;
                                    case "MinSize":
                                        out.println(messages.get('crud.help.minlength', it.annotation.value()))
                                        break;
                                    case "MaxSize":
                                        out.println(messages.get('crud.help.maxlength', it.annotation.value()))
                                        break;
                                    case "Range":
                                        out.println(messages.get('crud.help.range', it.annotation.min(), it.annotation.max()))
                                        break;
                                    case "Min":
                                        out.println(messages.get('crud.help.min', it.annotation.value()))
                                        break;
                                    case "Email":
                                        out.println(messages.get('crud.help.email'))
                                        break;
                                    case "Equals":
                                        out.println(messages.get('crud.help.equals', it.params.equalsTo))
                                        break;
                                    case "Future":
                                        if(it.params.reference) {
                                            out.println(messages.get('crud.help.after', it.params.reference))
                                        } else {
                                            out.println(messages.get('crud.help.future'))
                                        }
                                        break;
                                    case "Past":
                                        if(it.params.reference) {
                                            out.println(messages.get('crud.help.before', it.params.reference))
                                        } else {
                                            out.println(messages.get('crud.help.past'))
                                        }
                                        break;
                                }
                            }%
                        %{ } }%
                </span>
            </div>
        #{/}
    %{ } }%
#{/list}
</div>
